package com.hnevc.getpostnetdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodListActivity extends AppCompatActivity {

    ListView mListView;
    String[] foodNames;
    List<Food> foodList;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            MyBaseAdapter adapter = new MyBaseAdapter();
            mListView.setAdapter(adapter);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

        initView();
        initData();
        initEvent();

    }


    private void initView() {
        //获取布局文件中的控件
        mListView = (ListView) findViewById(R.id.id_lv_base);

    }

    private void initData() {
        foodList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String result = NetAccess.getFoods();
                    JSONArray jsonArray = new JSONArray(result);
                    for(int i = 0 ;i<jsonArray.length(); i++){
                        JSONObject object = jsonArray.getJSONObject(i);
                        Food food = new Food();
                        food.setFoodId(object.getInt("foodId"));
                        food.setFoodName(object.getString("foodName"));
                        food.setFoodPrice(object.getInt("foodPrice"));
                        food.setFoodDesc(object.getString("foodDesc"));
                        food.setFoodPic(object.getString("foodPic"));
                        foodList.add(food);

                    }
                    mHandler.sendEmptyMessage(101);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void initEvent() {

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(FoodListActivity.this, "ListView点击事件FROM" + position, Toast.LENGTH_SHORT).show();
            }
        });




    }


    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return foodList.size();
        }

        @Override
        public Object getItem(int position) {
            return foodList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                View view = LayoutInflater.from(FoodListActivity.this).inflate(R.layout.food_list_item, null);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(FoodListActivity.this, "ListView点击事件" + position, Toast.LENGTH_SHORT).show();
                    }
                });

                viewHolder = new ViewHolder();
                viewHolder.mTvFoodName = (TextView) view.findViewById(R.id.id_tv_foodName);
                viewHolder.mTvFoodPrice = (TextView) view.findViewById(R.id.id_tv_foodPrice);
                viewHolder.mIvFoodPic = (ImageView) view.findViewById(R.id.id_iv_foodPic);
                viewHolder.mTvFoodDesc = (TextView) view.findViewById(R.id.id_tv_foodDesc);
                view.setTag(viewHolder);

                convertView = view;
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final Food food = foodList.get(position);
            viewHolder.mTvFoodName.setText(food.getFoodName());
            viewHolder.mTvFoodPrice.setText(food.getFoodPrice()+"");
           /* //写线程下载图片
            new Thread(new Runnable() {
                @Override
                public void run() {
                    InputStream in = NetAccess.getImageFile(food.getFoodPic());
                    final Bitmap bitmap = BitmapFactory.decodeStream(in);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mIvFoodPic.setImageBitmap(bitmap);
                        }
                    });

                }
            }).start();*/
           String foodPicUrl = "http://10.1.46.115:8080/RestaurantServer/uploadFoodImage/"+food.getFoodPic();
            Picasso.with(FoodListActivity.this).load(foodPicUrl)
                .into(viewHolder.mIvFoodPic);

            viewHolder.mTvFoodDesc.setText(food.getFoodDesc());
            return convertView;
        }
    }

    class ViewHolder {
        TextView mTvFoodName;
        TextView mTvFoodPrice;
        TextView mTvFoodDesc;
        ImageView mIvFoodPic;
    }
}
